package cn.client

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import cn.client.data.UserStateManager
import cn.client.navigation.destinations.AppBottomScreens
import cn.client.navigation.destinations.Message
import cn.client.navigation.destinations.Myself
import cn.client.navigation.destinations.Overview
import cn.client.navigation.AppNavHost
import cn.client.navigation.destinations.Mindfulness
import cn.client.navigation.navigateSingleTopTo
import cn.client.ui.components.AppBottomBar
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private lateinit var viewModel: MainViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 使用 ViewModelProvider 获取 ViewModel 实例
        viewModel = ViewModelProvider(this)[MainViewModel::class.java]

        setContent {
            MainApp(viewModel)
        }
    }
}


/**
 * 主程序入口
 */
@Composable
fun MainApp(
    viewModel: MainViewModel
) {
    val navController = rememberNavController()
    // 解决标签页界面问题
    val currentBackStack by navController.currentBackStackEntryAsState()
    val currentDestination = currentBackStack?.destination
    val currentScreen = AppBottomScreens.find { it.route == currentDestination?.route } ?: Overview

    // 使用 collectAsState 观察 userId 的变化
    val userIdState = viewModel.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    val onConnectState = viewModel.onConnect.collectAsState(initial = false)
    val onConnect = onConnectState.value

    if (userId == -1) {
        viewModel.dispatch(MainViewModel.PageIntent.GetUser("获取本地用户缓存"))
    }
    if (!onConnect || userId != null) {
        if (userId != -1){
            viewModel.dispatch(MainViewModel.PageIntent.ConnectToWebSocket(uid = userId))
        }
    }


    Scaffold(
        bottomBar = {
            //  根据底部导航设置排除条件
            if (currentDestination?.hierarchy?.any
                { it.route == Overview.route || it.route == Mindfulness.route|| it.route == Message.route || it.route == Myself.route  } == true
            ) {
                AppBottomBar(
                    allScreens = AppBottomScreens,
                    onTabSelected = { newScreen ->
                        navController.navigateSingleTopTo(newScreen.route )
                    },
                    currentScreen = currentScreen
                )
            }
        }
    ) { innerPadding ->
        AppNavHost(
            navController = navController,
            modifier = Modifier.padding(innerPadding)
        )
    }
}
